<?php
/**
 * 后台用户模型类
 *
 * @author liang<lang@vip.deyi.com>
 * @since   2016/11/30 10:43
 * @copyright  deyi.com
 */
namespace Apps\Acp\Models;

use Apps\Common\Model\BaseModel;

/**
 * Class Manager
 * @package Apps\Acp\Models
 */
class Manager extends BaseModel
{
    // 定义此model访问的数据库名
    protected static $_DBNAME = 'yyx_fanli';

    // 定义此model访问的表名
    protected static $_TABLENAME = 'acp_manager';

    public function getListByConds($conditions, $offset, $limit)
    {
        list($values, $conds, $likeConds) = $this->assembleConditionsData($conditions);
        $where = $this->assembleComplexWhereSql($conds, $likeConds);

        $sql = 'SELECT * FROM ' . self::$_TABLENAME . $where . ' ORDER BY  id DESC ' . $this->assembleLimit($offset, $limit);

        return $this->select($sql, $values);
    }

    public function countListByConds($conditions)
    {
        list($values, $conds, $likeConds) = $this->assembleConditionsData($conditions);
        $where = $this->assembleComplexWhereSql($conds, $likeConds);

        $sql = 'SELECT COUNT(*) AS num FROM ' . self::$_TABLENAME . $where;

        return $this->selectCount($sql, $values);
    }

    //教练列表
    public function getCoachList($groupid, $offset, $limit)
    {
        $sql = 'SELECT id, username, phone, groupid FROM ' . self::$_TABLENAME . ' WHERE groupid=?' .
            $this->assembleLimit(
                $offset,
                $limit
            );
        return $this->select($sql, ['groupid' => $groupid])->toArray();
    }

    public function countNumGroupByGroupId($gids)
    {
        if (empty($gids)) {
            return [];
        }
        $sql = 'SELECT groupid,COUNT(*) AS num FROM ' . self::$_TABLENAME . ' WHERE groupid IN ' . $this->assemblePlaceholders($gids) . ' GROUP BY groupid';

        $res = $this->selectCols($sql, ['groupid', 'num'], $gids);

        $data = [];

        foreach ($res as $v) {
            $data[$v->groupid] = $v->num;
        }

        return $data;
    }
}
